﻿// -----------------------------------------------------------------------
// <copyright file="Utility.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace HashMaster.Agent.Neo
{
    using System;
    using HashMaster.Lib;
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Runtime.Serialization;
    using System.IO;
using System.Diagnostics;


    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class Utility
    {
        private static bool ClientNameSaved=false;

        public static string ServiceUrl{get;set;}

        public static ProcessPriorityClass HashcatPriority { get; set; }

        private static ClientSyncData clientData;
        public static ClientSyncData ClientData 
        {
            get 
            {
                return clientData;
            }
            set 
            {
                clientData = value;
                SaveAppSettings();
            }
        }

        ///监视间隔,单位秒
        public static int MonitorInterval;
        public static string HashcatPath;
        public static bool NeedForceStop { get; set; }

        static  Utility()
        {
            //载入配置信息
            ClientData = new ClientSyncData();
            LoadAppConfigs();
        }



        public static bool ClientSync()
        {
            NeedForceStop = false;
            string requestUrl = string.Format("{0}job/", ServiceUrl);
            DataContractSerializer ds = new DataContractSerializer(typeof(ClientSyncData));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
            WebResponse response;

            request.Method = "POST";
            request.ContentType = "application/xml";
            try
            {

                ds.WriteObject(request.GetRequestStream(), ClientData);
                response = request.GetResponse();

                    ClientSyncData tmp = (ClientSyncData)ds.ReadObject( response.GetResponseStream());
                    if (ClientData.JobId != tmp.JobId)
                    {
                        Console.WriteLine("\r\n>>Current Job: " + ClientData.JobId + " New Job: " + tmp.JobId);
                        NeedForceStop = true;
                    }
                    ClientData = tmp;
                
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            return false;

        }


        private static void LoadAppConfigs()
        {
            ServiceUrl = Properties.Settings.Default.ServiceUrl;
            ClientData.ClientName = Properties.Settings.Default.ClientId;
            MonitorInterval = 10;////////////int.Parse(Properties.Settings.Default.MonitorInterval);
            HashcatPath = Properties.Settings.Default.HashcatPath;
            ClientData.MacAddress = GetMacAddress();
            //处理 serviceUrl 未设置的情况 
            if (ServiceUrl == string.Empty)
            {

            }
        }

        private static void SaveAppSettings()
        {
            
            
            if ( ClientData.ClientName!=null&ClientNameSaved==false)
            {
                Properties.Settings.Default.ClientId = ClientData.ClientName;
                Properties.Settings.Default.Save();
                ClientNameSaved = true;
            }

        }



        /// <summary>
        /// returns the mac address of the first operation nic found.
        /// </summary>
        /// <returns></returns>
        private static string GetMacAddress()
        {
            string macAddresses = "";

            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nic.OperationalStatus == OperationalStatus.Up)
                {
                    macAddresses += nic.GetPhysicalAddress().ToString();
                    break;
                }
            }
            return macAddresses;
        }
    }
}
